package cn.getech.data.development.controller;

import cn.getech.data.development.model.vo.TablePartitionStatisticsVO;
import cn.getech.data.development.service.TablePartitionStatisticsService;
import cn.getech.data.development.utils.sched.TablePartitionStatisticsTask;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;

@Slf4j
@Api(tags = "数据表分区字段统计信息")
@RestController
@RequestMapping("/bdp/tablePartitionStatic")
public class TablePartitionStatisticsController {

    @Autowired
    private TablePartitionStatisticsService tablePartitionStatisticsService;

    @Autowired
    private TablePartitionStatisticsTask tablePartitionStatisticsTask;

    @ApiOperation("根据表id获取分区字段统计信息")
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = TablePartitionStatisticsVO.class)})
    @GetMapping("/{tableId}/{sort}")
    @SysLog(value="根据表id获取表的统计信息",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R saveOfYesterday(@ApiParam(name = "tableId", value = "表id", required = true) @PathVariable("tableId") Integer tableId,
                             @ApiParam(name = "sort", value = "1：总量升序。2：降序", required = true) @PathVariable("sort") Integer sort) {
        return R.okWithData(tablePartitionStatisticsService.sizeTopTenRanking(tableId, sort));
    }

    @ApiOperation("根据表id获取分区字段统计信息")
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = TablePartitionStatisticsVO.class)})
    @GetMapping("/partitionStoreList")   ///bdp/tablePartitionStatic/partitionStoreList
    @SysLog(value="根据表id获取分区字段统计信息",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R saveOfYesterday(Integer tableId,String unit) {
        return R.okWithData(tablePartitionStatisticsService.selectPartitionStoreList(tableId, unit));
    }


    @ApiOperation("执行统计：统计表分区信息")
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = TablePartitionStatisticsVO.class)})
    @GetMapping("/statisticsPartitionRowsAndSize")//  /bdp/tablePartitionStatic/statisticsPartitionRowsAndSize
    @SysLog(value="执行统计：统计表分区信息",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R statisticsPartitionRowsAndSize() {
        log.info("开始统计表分区信息, date:{}", DateUtil.now());
        tablePartitionStatisticsTask.tablePartitionStatiticsTask("1");
        log.info("统计表分区信息结束, date:{}", DateUtil.now());
        return R.ok();
    }

}
